﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ChuanYe.CountyFireManagement.Api.Models;
using ChuanYe.CountyFireManagement.BLL;
using ChuanYe.CountyFireManagement.EF.MySQL.Models;
using ChuanYe.CountyFireManagement.Model;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace ChuanYe.CountyFireManagement.Api.Controllers
{
    /// <summary>
    /// 省市县区镇 级联菜单
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class LinchpinController : ControllerBase
    {

        /// <summary>
        /// 根据ID 获取城市信息
        /// </summary>
        /// <param name="obj">obj.LinchpinID</param>
        /// <returns></returns>
        [HttpPost]
        public ResultDto<dynamic> Get(LinchpinSearchRequest obj)
        {
            ResultDto<dynamic> result = new ResultDto<dynamic>();
            try
            {
                long linchpinID = 101;
                if (obj != null && obj.LinchpinId != 0)
                {
                    linchpinID = obj.LinchpinId;
                }
                result.FirstParam = LinchpinBLL.Instance.GetLinchpin(linchpinID);
            }
            catch (Exception ex)
            {
                result.Status = 500;
                result.Message = "/Linchpin/Get Server Error";
                LogHelper.Error(result.Message,ex);
            }
            return result;
        }

        /// <summary>
        /// 根据ID 获取下级城市信息集合
        /// </summary>
        /// <param name="obj">obj.LinchpinID</param>
        /// <returns></returns>
        [HttpPost]
        public ResultDto<dynamic> GetSub(LinchpinSearchRequest obj)
        {
            ResultDto<dynamic> result = new ResultDto<dynamic>();
            try
            {
                long linchpinID = 101;//默认北京市
                if (obj != null && obj.LinchpinId != 0)
                {
                    linchpinID = obj.LinchpinId;
                }
                result.FirstParam = LinchpinBLL.Instance.GetSubLinchpin(linchpinID);

            }
            catch (Exception ex)
            {
                result.Status = 500;
                result.Message = "/Linchpin/GetSub Server Error";
                LogHelper.Error(result.Message,ex);
            }
            return result;
        }

        /// <summary>
        /// 推算出用户或者某一权限的用户最近使用大区
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ResultDto<dynamic> GetUserAreaProbability(UserAreaProbabilitySearchRequest obj)
        {
            ResultDto<dynamic> result = new ResultDto<dynamic>();
            try
            {
                Userareaprobability userAreaProbability = LinchpinBLL.Instance.GetUserAreaProbability(obj.UserId, obj.RoleId);
                result.FirstParam = new {
                    AddressCounty=userAreaProbability?.AddressCounty,
                    AddressTown = userAreaProbability?.AddressTown,
                    AddressVillage = userAreaProbability?.AddressVillage,
                };
            }
            catch (Exception ex)
            {
                result.Status = 500;
                result.Message = "/Linchpin/UserAreaProbability Server Error";
                LogHelper.Error(result.Message,ex);
            }
            return result;
        }

    }
}